package com.example.springboot02amqp.vo;

import com.example.springboot02amqp.constant.ResultCodeConstant;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.util.StringUtils;

/**
 * Created with IntelliJ IDEA.
 * User: 喵喵~
 * Date: 2022/4/4
 * Time: 18:34
 * Description:
 */
public class BaseResultVO<T> {
    private static final String EMPTY = "";

    @JsonProperty("isSuccess")
    private Boolean success;

    private String code;

    private String message;

    private T data;

    public static <T> BaseResultVO success(String message, T data) {
        BaseResultVO<T> baseResultVO = new BaseResultVO<>();
        baseResultVO.setSuccess(true);
        baseResultVO.setCode(ResultCodeConstant.SUCCESS);
        if(StringUtils.isEmpty(message)){
            baseResultVO.setMessage(EMPTY);
        }else{
            baseResultVO.setMessage(message);
        }
        baseResultVO.setData(data);
        return baseResultVO;
    }

    public static BaseResultVO success(String message) {
        return success(message, null);
    }

    public static <T> BaseResultVO success(T data) {
        return success("成功", data);
    }


    public static <T> BaseResultVO success() {
        return success(null);
    }

    public static <T> BaseResultVO fail(String message, T data) {
        BaseResultVO<T> baseResultVO = new BaseResultVO<>();
        baseResultVO.setSuccess(false);
        baseResultVO.setCode(ResultCodeConstant.FAIL);
        if(StringUtils.isEmpty(message)){
            baseResultVO.setMessage(EMPTY);
        }else{
            baseResultVO.setMessage(message);
        }
        baseResultVO.setData(data);
        return baseResultVO;
    }

    public static BaseResultVO fail(String message) {
        return fail(message, null);
    }

    public static <T> BaseResultVO fail(T data) {
        return fail("业务处理异常，请求失败", data);
    }

    public static <T> BaseResultVO fail() {
        return fail(null);
    }

    public static <T> BaseResultVO error(String code, String message, T data) {
        BaseResultVO<T> baseResultVO = new BaseResultVO<>();
        baseResultVO.setSuccess(false);
        baseResultVO.setCode(code);
        baseResultVO.setMessage(message);
        baseResultVO.setData(data);
        return baseResultVO;
    }

    public static BaseResultVO error(String code, String message) {
        return error(code, message, null);
    }

    public static <T> BaseResultVO error(String code, T data) {
        return error(code, null, data);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "{\"BaseResultVO\":{"
                + "\"success\":"
                + success
                + ",\"code\":\""
                + code + '\"'
                + ",\"message\":\""
                + message + '\"'
                + ",\"data\":"
                + data
                + "}}";

    }
}
